package com.zaofan.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

public class JsonUtils {

	public static <T> void writeSuccessPageJson(String msg, List<T> datas, Long count, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", true);
		data.put("msg", msg);
		if (datas == null) {
			data.put("data", Collections.EMPTY_LIST);
			data.put("total", 0);
			data.put("pageTotal", 0);
		} else {
			data.put("data", datas);
			data.put("total", count);
			data.put("pageTotal", datas.size());
		}
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static <T> void writeSuccessPageJson(String msg, List<T> datas, Long count,int cityId, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", true);
		data.put("msg", msg);
		if (datas == null) {
			data.put("data", Collections.EMPTY_LIST);
			data.put("total", 0);
			data.put("pageTotal", 0);
		} else {
			data.put("cityId",cityId);
			data.put("data", datas);
			data.put("total", count);
			data.put("pageTotal", datas.size());
		}
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static <T> void writeSuccessPageJson(String msg, T datas, Long count, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", true);
		data.put("msg", msg);
		if (datas == null) {
			data.put("data", Collections.EMPTY_LIST);
			data.put("total", 0);
			data.put("pageTotal", 0);
		} else {
			data.put("data", datas);
			data.put("total", count);
			data.put("pageTotal", 0);
		}
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static <T> void writeSuccessJson(String msg, List<T> datas, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", true);
		data.put("msg", msg);
		if (datas == null) {
			data.put("data", Collections.EMPTY_LIST);
			data.put("total", 0);
			data.put("pageTotal", 0);
		} else {
			data.put("data", datas);
			data.put("pageTotal", datas.size());
			data.put("total", datas.size());
		}
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static <T> void writeSuccessJson(String msg, T s, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", true);
		data.put("msg", msg);
		data.put("total", 0);
		if (s == null) {
			data.put("data", new Object());
		} else {
			data.put("data", s);
		}
		data.put("pageTotal", 0);
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static void writeFailJson(String msg, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", false);
		data.put("msg", msg);
		data.put("total", 0);
		data.put("data", new Object());
		data.put("pageTotal", 0);
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static void writeBeanFailJson(String msg, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", false);
		data.put("msg", msg);
		data.put("total", 0);
		data.put("data", new Object());
		data.put("pageTotal", 0);
		writeData(response, GsonUtils.getInstance().toJson(data));
	}
	
	public static void writeBeanFailJson(String msg, Integer errCode,HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", false);
		data.put("msg", msg);
		data.put("errCode", errCode);
		data.put("total", 0);
		data.put("data", new Object());
		data.put("pageTotal", 0);
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static void writeListFailJson(String msg, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", false);
		data.put("msg", msg);
		data.put("total", 0);
		data.put("data", Collections.EMPTY_LIST);
		data.put("pageTotal", 0);
		writeData(response, GsonUtils.getInstance().toJson(data));
	}

	public static void writeFailJsonWithState(String msg, int state, HttpServletResponse response) {
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("success", false);
		data.put("msg", msg);
		data.put("total", 0);
		data.put("data", state);
		data.put("pageTotal", 0);
		writeData(response, GsonUtils.getInstance().toJson(data));
	}
     public static  void writeSuccessJsonActionPage(String msg,String flag,HttpServletResponse response){
		 Map<String, Object> data = new HashMap<String, Object>();
		 data.put("success", true);
		 data.put("msg", msg);
		 data.put("url", msg);
		 data.put("flag", flag);
		 data.put("data", Collections.EMPTY_LIST);
		 writeData(response, GsonUtils.getInstance().toJson(data));
	 }
	public static void writeData(HttpServletResponse response, String data) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter pw = response.getWriter();
			pw.write(data);
			pw.flush();
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
